package src.project.contacts;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.RawContacts;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.ViewGroup.LayoutParams;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;


public class ContactQuery extends Activity implements OnSharedPreferenceChangeListener {

	private TableLayout table;
	private SharedPreferences preferences;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        table = (TableLayout)findViewById(R.id.table1);
        preferences = PreferenceManager.getDefaultSharedPreferences(this);
        preferences.registerOnSharedPreferenceChangeListener(this);
        
        String[] dbColumns = {Phone.NUMBER, Data.RAW_CONTACT_ID};
        
        int[] columnTypes = {0};
        
        addData(Phone.CONTENT_URI, dbColumns, columnTypes, null, null);
    }
    
	private void addData(Uri uri, String[] dbColumns, int[] columnTypes, String conditions, String[] conditionArgs)
    {
    	ContentResolver cr = getContentResolver();
    	Cursor cursor = cr.query(uri, dbColumns, conditions, conditionArgs, null);
    	String raw_id = ""; 
    	
    	while(cursor.moveToNext())
    	{
    		for(int i = 0; i < dbColumns.length; i++)
    		{
    			if(dbColumns[i].equals(Data.RAW_CONTACT_ID))
    			{
    				Log.v("Inside First Loop", "DBColumns equaled Raw Contact ID");
    				raw_id = cursor.getString(cursor.getColumnIndex(Data.RAW_CONTACT_ID));
    			}
    		}
    	}
    	cursor.close();
    	
    	Log.v("Raw ID", raw_id);
    	
		cursor = getContentResolver().query(Phone.CONTENT_URI,
				new String[]{Contacts.DISPLAY_NAME, Phone.NUMBER},
				RawContacts.CONTACT_ID + "=?",
				new String[]{raw_id}, null);
    	
    	while(cursor.moveToNext())
    	{
    		Log.v("Second Cursor", "Cursor is not null");
    		TableRow row = new TableRow(this);
    		
    		for(int i = 0; i < cursor.getColumnCount(); i++)
    		{
    			Log.v("Inside 2nd cursor for loop", "2nd Cursor has information");
    			Log.v("Column index " + String.valueOf(i) + ":", cursor.getString(i));
    			TextView text = new TextView(this);
    			text.setText(cursor.getString(i));
    			text.setTextSize(18);
    			text.setGravity(Gravity.CENTER_HORIZONTAL);
    			text.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, 
						   									   LayoutParams.WRAP_CONTENT, 
						   									   1f));
    			row.addView(text);
    		}
    		table.addView(row);
    	}
    	
    	cursor.close();
    }
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu){
    	MenuInflater inflator = getMenuInflater();
    	inflator.inflate(R.menu.menu, menu);
    	return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
    	switch(item.getItemId()){
    	case R.id.preferences:
    		Intent i = new Intent(this, Preferences.class);
    		startActivity(i);
    		break;
    	}
    	return true;
    }

	@Override
	public void onSharedPreferenceChanged(SharedPreferences arg0, String arg1) {
		// TODO Auto-generated method stub
		
	}
}


